<?php


namespace app\home\service;


use app\common\model\AgentLavel;
use app\common\model\UserModel;

class InspectProgramService
{

    //定时任务检查是否该用户是会员 却有分销比例和分销信息
    public function branch_agent()
    {
        //修改等级表  所有会员的分销比例
        AgentLavel::alias('a')->join(UserModel::class . ' b', 'a.agent_id=b.id', 'left')->where('type', 2)->update(['rate' => 0, 'agent_rate' => 0]);
        //修改所有会员的分销比例
        //UserModel::where('type',2)->update(['agent_rate'=>0]);
        //删除所有会员的分销比例数据
        return $this;
    }

    //定时任务检查是否有超过的 分销
    public function proportion()
    {
        //查询顶级代理
        $dingji = AgentLavel::where('agent_pid', 0)->field('agent_id,rate')->select()->toArray();
        if (!is_array($dingji)) return $this;
        //得到所有非顶级代理商
        $agent = AgentLavel::field('agent_id,rate,agent_pid')->group('agent_id')->select();
        if (!is_array($agent)) return $this;

        foreach ($dingji as $key => $value) {
            $dingji['prate'] = $dingji['rate'];
            foreach ($agent as $k => $val) {
                if ($dingji['agent_id'] == $agent['agent_pid']){
                    $dingji['prate'] +=$agent['rate'];
                }
            }
        }
    }
}